﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using DistributeTransaction.Models;

namespace DistributeTransaction.DAL
{
    public class WatchListRepository : GenericRepository<WatchList>
    {
        public WatchListRepository(DatabaseContext context)
            : base(context)
        {

        }

        internal IEnumerable<WatchList> GetWatchList(Guid userId)
        {
            const string query = @"SELECT W.[ProductID], W.[UserId], W.[UnitPrice], W.[Coupon],
		P.ProductName, P.QuantityPerUnit, S.CompanyName
  FROM [dbo].[WatchList] W
  JOIN [dbo].[Product] P  ON W.[ProductID] = P.[ProductID]
  JOIN [dbo].[Suppliers] S ON P.SupplierID = S.SupplierID
WHERE W.UserId = @p0";
            return GetWithRawSql(query, userId).ToList();
        }

        internal void UpdateWatchList(IEnumerable<int> productIds, Guid userId)
        {
            IEnumerable<WatchList> items = GetWatchList(userId);
            List<int> list = new List<int>();
            foreach (WatchList item in items)
            {
                if (productIds.Contains(item.ProductID))
                {
                    list.Add(item.ProductID);
                }
            }

            foreach (int index in productIds)
            {
                if (!list.Contains(index))
                {
                    WatchList item = new WatchList();
                    item.UserId = userId;
                    item.ProductID = index;
                    Insert(item);
                }
            }
        }

        protected override void Insert(WatchList entity)
        {
            const string sql = @"dbo.Insert_WatchList";
            
            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = entity.UserId;

            SqlParameter pProdId = new SqlParameter();
            pProdId.ParameterName = "ProductId";
            pProdId.Direction = ParameterDirection.Input;
            pProdId.DbType = DbType.Int32;
            pProdId.Value = entity.ProductID;

            ExecuteStoreProcedure(sql, pUserId, pProdId);
        }

        internal void Remove(IEnumerable<int> productIds, Guid userId)
        {
            const string sql = @"dbo.Delete_WatchList";

            SqlParameter pUserId = new SqlParameter();
            pUserId.ParameterName = "UserId";
            pUserId.Direction = ParameterDirection.Input;
            pUserId.DbType = DbType.Guid;
            pUserId.Value = userId;

            foreach (int id in productIds)
            {
                SqlParameter pProdId = new SqlParameter();
                pProdId.ParameterName = "ProductId";
                pProdId.Direction = ParameterDirection.Input;
                pProdId.DbType = DbType.Int32;
                pProdId.Value = id;

                ExecuteStoreProcedure(sql, pUserId, pProdId);
            }
        }
    }
}